<?php 
class ControllerPassword extends Controller { 
	var $error = array();
	        
	function index() {
		$response =& $this->locator->get('response');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template'); 
		$module   =& $this->locator->get('module');

		$language->load('controller/password.php');

		$template->set('title', $language->get('heading_title'));

		$template->set('content', $this->getFORM());

		$template->set($module->fetch());

		$response->set($template->fetch('layout.tpl'));
	}
	
	function update() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$database =& $this->locator->get('database');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template');
		$session  =& $this->locator->get('session');
		$module   =& $this->locator->get('module');
		$cache    =& $this->locator->get('cache');

		$language->load('controller/password.php');

		$template->set('title', $language->get('heading_title'));

		if (($request->isPost()) && ($this->validateForm())) {
			$sql = "update siaf_personas set CLAVE = '?' where Persona = '?'";
			$database->query($database->parse($sql, $request->get('password', 'post'), $request->get('nro_doc', 'get')));

			$cache->delete('password');
			
			$session->set('message', "Se ha actualizado la contrase&ntilde;a");
			
			$response->redirect($url->ssl('password'));
		}

		$template->set('content', $this->getForm());

		$template->set($module->fetch());

		$response->set($template->fetch('layout.tpl'));
	}
	
	function getFORM() { 
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$url      =& $this->locator->get('url');
		$language =& $this->locator->get('language');
		$template =& $this->locator->get('template');
		$module   =& $this->locator->get('module');
		$user     =& $this->locator->get('user');
		$session  =& $this->locator->get('session');

    	$language->load('controller/password.php');
    
		$template->set('title', $language->get('heading_title'));

		if (!$user->isLogged()) {
			$response->redirect($url->ssl('login'));
		}
   	
    	$view = $this->locator->create('template');

    	$view->set('heading_title', $language->get('heading_title'));

    	$view->set('error', @$this->error['message']);
    	$view->set('message', $session->get('message'));
    	$session->delete('message');
    	
    	$view->set('entry_newpassword', $language->get('entry_newpassword'));
    	$view->set('entry_newpasswordagain', $language->get('entry_newpasswordagain'));

    	$view->set('button_aceptar', $language->get('button_aceptar'));
    	$view->set('button_cancelar', $language->get('button_cancelar'));
		
    	$view->set('cancel', $url->ssl('home'));
    	$view->set('action',$url->ssl('password', 'update', array('nro_doc' => $user->getPERSONA())));

		return $view->fetch('content/password.tpl');

  	}
	
		function validateForm() {
		$request  =& $this->locator->get('request');
		$user     =& $this->locator->get('user');
		$language =& $this->locator->get('language');


		if ((strlen($request->get('password', 'post')) < 1) ) {
			$this->error['message'] = "Debe ingresar una contrase&ntilde;a";
		}

		if (strlen($request->get('password', 'post')) != strlen($request->get('newpassword', 'post')) ) {
			$this->error['message'] = "Las contrase&ntilde;as no coinciden";
		}

		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
	function isLogged() {
		$user =& $this->locator->get('user');
		
		if (!$user->isLogged()) {
			return $this->forward('login', 'index');
		}
	}
	
}  
?>
